//
//  MLMonthDatePickerButton.m
//  InventoryTool
//
//  Created by molon on 5/26/14.
//  Copyright (c) 2014 Molon. All rights reserved.
//

#import "MLMonthDatePickerButton.h"
#import "MLMonthDatePicker.h"
#import "UIWindow+MLKit.h"

#define kBlackOverlayViewAnimateDuration .25f

@interface MLMonthDatePickerButton()<MLMonthDatePickerDelegate>

@property(strong,nonatomic,readwrite) UIToolbar *inputAccessoryView;
@property(strong,nonatomic,readwrite) MLMonthDatePicker *inputView;
@property(nonatomic,strong) UIView *blackOverlayView;
@property(nonatomic,strong) UILabel *monthPickerTitleLabel;

@end

@implementation MLMonthDatePickerButton

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setUp];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    //添加一个点击可成为第一响应者的Target
    [super addTarget:self action:@selector(becomeFirstResponder) forControlEvents:UIControlEventTouchUpInside];
}

//覆盖了，不让用户外部可以添加Target
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{
    return;
}

#pragma mark - input
- (UIToolbar *)inputAccessoryView
{
    if(!_inputAccessoryView){
        UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.inputView.frame.size.width, 44)];
        UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel)];
        if (self.isCanReset) {
            left = [[UIBarButtonItem alloc]initWithTitle:@"不限" style:UIBarButtonItemStyleDone target:self action:@selector(reset)];
        }
        UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(done)];
        toolBar.items = @[left,spaceItem,right];
        
        toolBar.backgroundColor = [UIColor whiteColor];
        _inputAccessoryView = toolBar;
    }
    return _inputAccessoryView;
}

- (MLMonthDatePicker *)inputView
{
    if(!_inputView)
    {
        MLMonthDatePicker *monthPicker = [[MLMonthDatePicker alloc]init];
        monthPicker.monthPickerDelegate = self;
        monthPicker.backgroundColor = [UIColor whiteColor];
        //日期最大为第二天
        monthPicker.maximumDate = [NSDate dateWithTimeIntervalSince1970:[[NSDate date] timeIntervalSince1970]+60*60*24];
        
        _inputView = monthPicker;
    }
    return _inputView;
}

#pragma mark - date change delegate

- (void)mlMonthDatePicker:(MLMonthDatePicker*)picker didSelectDate:(NSDate*)date
{
    if (self.delegate&&[self.delegate respondsToSelector:@selector(didScrollToDate:ofMonthPickerButton:)]) {
        [self.delegate didScrollToDate:date ofMonthPickerButton:self];
    }
}

#pragma mark - other view
- (UILabel*)monthPickerTitleLabel
{
    if (!_monthPickerTitleLabel) {
        UILabel *label = [[UILabel alloc]init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        
        [self.inputAccessoryView addSubview:label];
        
        _monthPickerTitleLabel = label;
        
        _monthPickerTitleLabel.frame = self.inputAccessoryView.bounds;
    }
    return _monthPickerTitleLabel;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.monthPickerTitleLabel.frame = self.inputAccessoryView.bounds;
}


#pragma mark - setter and getter

- (void)setMonthPickerTitle:(NSString *)monthPickerTitle
{
    _monthPickerTitle = [monthPickerTitle copy];
    
    self.monthPickerTitleLabel.text = monthPickerTitle;
}

- (MLMonthDatePicker*)monthPicker
{
    return self.inputView;
}

#pragma mark toolBar event
-(void)cancel
{
    [self resignFirstResponder];
}

- (void)reset
{
    if (self.delegate&&[self.delegate respondsToSelector:@selector(resetWithMonthPickerButton:)]) {
        [self.delegate resetWithMonthPickerButton:self];
    }
    
    self.monthPicker.date = [NSDate date];
    [self resignFirstResponder];
}

- (void)done
{
    if (self.delegate&&[self.delegate respondsToSelector:@selector(doneWithDate:ofMonthPickerButton:)]) {
        [self.delegate doneWithDate:self.monthPicker.date ofMonthPickerButton:self];
    }
    
    [self resignFirstResponder];
}

#pragma mark - responder and blackOverlayView
- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (BOOL)becomeFirstResponder
{
    if ([self isFirstResponder]) {
        return YES;
    }
    BOOL result = [super becomeFirstResponder];
    if (result) {
        if (self.blackOverlayView) {
            [self.blackOverlayView removeFromSuperview];
            self.blackOverlayView = nil;
        }
        //添加一个蒙版黑色View
        UIWindow* window = [UIWindow currentNormalWindow];
        UIView *blackView = [[UIView alloc]initWithFrame:window.bounds];
        blackView.backgroundColor = [UIColor blackColor];
        blackView.userInteractionEnabled = YES;
        [window addSubview:self.blackOverlayView = blackView];
        
        //对其添加点击事件
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]init];
        tapGesture.numberOfTapsRequired = 1;
        tapGesture.numberOfTouchesRequired = 1;
        [tapGesture addTarget:self action:@selector(tapBlackOverlayViewWithTapGesture:)];
        [self.blackOverlayView addGestureRecognizer:tapGesture];
        
        blackView.layer.opacity = 0.09f;
        [UIView animateWithDuration:kBlackOverlayViewAnimateDuration animations:^{
            blackView.layer.opacity = 0.30f;
        }];
        
        if (self.delegate&&[self.delegate respondsToSelector:@selector(becomeFirstResponderWithMonthPickerButton:)]) {
            [self.delegate becomeFirstResponderWithMonthPickerButton:self];
        }
    }
    return result;
}

- (BOOL)resignFirstResponder
{
    BOOL result = [super resignFirstResponder];
    if (result) {
        [self hideBlackOverlayView];
        
        if (self.delegate&&[self.delegate respondsToSelector:@selector(resignFirstResponderWithMonthPickerButton:)]) {
            [self.delegate resignFirstResponderWithMonthPickerButton:self];
        }
    }
    return result;
}

#pragma mark - tap event
- (void)tapBlackOverlayViewWithTapGesture:(UITapGestureRecognizer*)tapGesture
{
    [self cancel];
}

#pragma mark - touchUpInSide
- (void)touchUpInSide
{
    [self becomeFirstResponder];
}

#pragma mark - other
- (void)dealloc
{
    [self hideBlackOverlayView];
}

- (void)hideBlackOverlayView
{
    if (self.blackOverlayView) {
        UIView *blackOverlayView = self.blackOverlayView;
        self.blackOverlayView = nil;
        [UIView animateWithDuration:kBlackOverlayViewAnimateDuration animations:^{
            blackOverlayView.layer.opacity = 0.01f;
        } completion:^(BOOL finished) {
            [blackOverlayView removeFromSuperview];
        }];
    }
}

@end
